package br.com.rglariguet.rgportal.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.rglariguet.rgportal.Cliente;
import br.com.rglariguet.rgportal.Hora;
import br.com.rglariguet.rgportal.dao.ClienteDAO;
import br.com.rglariguet.rgportal.dao.EnderecoDAO;
import br.com.rglariguet.rgportal.dao.HoraDAO;
import br.com.rglariguet.rgportal.util.HoraUtil;

@Controller
@RequestMapping("/admin/horas")
public class HoraController {
	
	@Autowired
	HoraDAO horaDAO;
	
	@Autowired
	ClienteDAO clienteDAO;

	@Autowired
	EnderecoDAO enderecoDAO;
	
	@RequestMapping(value="/novo",   method = RequestMethod.GET)
	public ModelAndView novo(){
		ModelAndView mv = new ModelAndView("/admin/horas/novoLancamentoHoras");
		mv.addObject("clientesList", preencherTodosClientes());
		mv.addObject("hora", new Hora());
		return mv;
	}
	
	@RequestMapping(value="/salvar",  method = RequestMethod.POST)
	public String salvar(@Valid Hora hora, BindingResult br, ModelMap model){
		if (br.hasErrors()) {
			model.addAttribute("clientesList", preencherTodosClientes());
			return "/admin/horas/novoLancamentoHoras";
		}
		Cliente cliente = clienteDAO.buscarPorId(hora.getIdCliente());
		hora.setCliente(cliente);
		String horaAux = HoraUtil.subtraiHoras(hora.getHoraSaida(), hora.getHoraEntrada());
		String tempoTrab = HoraUtil.subtraiHoras(horaAux, hora.getTempoAlmoco());
		hora.setTotalHorasTrabalhadas(tempoTrab);
		horaDAO.salvar(hora);
		return "redirect:/admin/horas/listar";
	}
	
	@RequestMapping(value="/alterar",  method = RequestMethod.POST)
	public ModelAndView alterar(Hora hora){
		horaDAO.alterar(hora);
		ModelAndView mv = new ModelAndView("redirect:/admin/horas/listar");
		return mv;
	}
	
	@RequestMapping(value="/editar/{id}",  method = RequestMethod.GET)
	public ModelAndView editar(@PathVariable Long id){
		Hora hora = horaDAO.buscarPorId(id);
		ModelAndView mv = new ModelAndView("/admin/horas/editarLancamentoHoras");
		mv.addObject(hora);
		mv.addObject("clientesList", preencherTodosClientes());
		return mv;
	}
	
	@RequestMapping(value="/excluir/{id}", method=RequestMethod.GET)	
	public String excluir(@PathVariable Long id) {
		Hora hora = horaDAO.buscarPorId(id);
		horaDAO.excluir(hora);
		return "redirect:/admin/horas/listar";
	}
	
	@RequestMapping("/listar")
	public String listarHoras(ModelMap model, Hora hora){
		List<Hora> horas = horaDAO.listarTodos();
		String totalGeralHoras = "00:00";
		for (int i = 0; i < horas.size(); i++) {
			totalGeralHoras = HoraUtil.somaHoras(horas.get(i).getTotalHorasTrabalhadas(), totalGeralHoras);			
		}
		model.addAttribute("totalGeralHoras", totalGeralHoras);
		model.addAttribute("horas", horas);
		return "admin/horas/listaLancamentoHoras";
	}
	
	protected Map<String, String> preencherTodosClientes() {
		Map<String,String> clientesList = new LinkedHashMap<String,String>();
		List<Cliente> clientes = clienteDAO.listarTodos();
		
		for (Cliente cliente : clientes) {
			clientesList.put(cliente.getId().toString(), cliente.getNomeFantasia());
		}
		return clientesList;
	}
}
